package com.t2c.tibetantochinese.wordstudy;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.realmModel.Phrase;
import com.t2c.tibetantochinese.utils.CommonUtil;

import java.io.IOException;

import io.realm.RealmResults;

/**
 * Created by zoulu on 16/6/26.
 * adapter
 */
public class TibetanAdapter extends BaseAdapter{
    private Context mContext;
    private RealmResults<Phrase> list;
    private MediaPlayer mediaPlayer ;

    public TibetanAdapter(Context context,RealmResults<Phrase> data){
        this.mContext = context;
        this.list = data;
        mediaPlayer = new MediaPlayer();
    }



    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null ) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.tibetanstudy_item, null);
            holder =new Holder();
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.iv_sound = (ImageView) convertView.findViewById(R.id.iv_sound);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.name.setTypeface(CommonUtil.getTibetanType(mContext));
        holder.name.setText(list.get(position).getTibetcontent());
        holder.iv_sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playAudio(position,mContext);
            }
        });
        return convertView;
    }

    private class Holder{
        TextView name;
        ImageView iv_sound;
    }

    /**
     * 播放语音
     */
    private void playAudio(int pos ,Context mContext){
        try {
            mediaPlayer.reset();
            AssetFileDescriptor descriptor = CommonUtil.getAudioPath(mContext,list.get(pos).getAudio().getTibetaudiopath());
            mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
